<template>
	<view class="page" :style="loading ? 'opacity: 0;' : 'opacity: 1;'">
		<!--  v-model="dataList" @query="queryList" -->
		<pageTxt>
			<z-paging ref="paging" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<!-- 头部 -->
				<template slot="top">
					<view class="navbar-box">
						<u-navbar title="勘场报告" :autoBack="true" :fixed="false"></u-navbar>
					</view>
				</template>
				<view class="add-content ios">
					<u--form labelPosition="top" :model="formData" ref="uForm" labelWidth="auto">
						<view class="form-box">
							<u-form-item labelPosition="top" labelWidth="100%" required label="现场图片 (.jpg,.png,.gif)" borderBottom>
								<file-upload :filesList="list" :type="3" @afterRead="afterRead('scene_file', $event)" @delete="deletePic('scene_file', $event)"></file-upload>
							</u-form-item>

							<u-form-item labelPosition="top" labelWidth="100%" required label="勘察报告 (.jpg,.png,.gif,.xlsx,.xls)" borderBottom>
								<file-upload
									:filesList="list"
									:typeList="['jpg', 'png', 'gif', 'xlsx', 'xls']"
									@afterRead="afterRead('report_file', $event)"
									@delete="deletePic('report_file', $event)"
								></file-upload>
							</u-form-item>

							<u-form-item labelPosition="top" labelWidth="100%" required label="视频 (.mp4,.3GP,.WMV)" borderBottom>
								<file-upload :filesList="list" :type="2" @afterRead="afterRead('video_file', $event)" @delete="deletePic('video_file', $event)"></file-upload>
							</u-form-item>

							<u-form-item labelPosition="top" labelWidth="100%" required label="手绘图 (.jpg,.png,.gif)" borderBottom>
								<file-upload :filesList="list" :type="3" @afterRead="afterRead('sketch_file', $event)" @delete="deletePic('sketch_file', $event)"></file-upload>
							</u-form-item>

							<u-form-item labelPosition="top" labelWidth="100%" label="备注">
								<u--textarea
									disableDefaultPadding
									v-model="formData.remark"
									border="none"
									placeholder="备注"
									:placeholderStyle="{ fontSize: '13.5px', color: '#BFC4CC' }"
								></u--textarea>
							</u-form-item>
						</view>
					</u--form>
				</view>
				<view class="footer ios">
					<u-button
						text="提交"
						@click="submitFunc"
						customStyle="width: 702rpx;height: 88rpx;background: #3B75FF;border-radius: 16rpx;font-weight: 500;font-size: 28rpx;color: #FFFFFF;margin:20rpx 24rpx;"
					></u-button>
				</view>
			</z-paging>
		</pageTxt>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: false,
			tjkcxxRuleForm: {
				scene_file: [],
				report_file: [],
				video_file: [],
				sketch_file: [],
				remark: '',
				project_id: ''
			}
		};
	},
	onLoad(option) {
		this.tjkcxxRuleForm.project_id = option.id || '';
	},
	methods: {
		afterRead(type, files) {
			files.map((item) => {
				let obj = {
					url: item.url
				};
				this.tjkcxxRuleForm[type].push(obj);
			});
		},
		deletePic(type, e) {
			this.tjkcxxRuleForm[type].splice(e.index, 1);
		},
		async submitFunc() {
			let tjkcxxRuleForm = this.tjkcxxRuleForm;
			let data = {
				remark: tjkcxxRuleForm.remark,
				project_id: tjkcxxRuleForm.project_id
			};
			if (!tjkcxxRuleForm.scene_file.length) {
				uni.showToast({
					title: '请选择现场图片',
					icon: 'error'
				});
				return;
			}
			if (!tjkcxxRuleForm.report_file.length) {
				uni.showToast({
					title: '请选择勘察报告',
					icon: 'error'
				});
				return;
			}
			if (!tjkcxxRuleForm.video_file.length) {
				uni.showToast({
					title: '请选择视频',
					icon: 'error'
				});
				return;
			}
			if (!tjkcxxRuleForm.sketch_file.length) {
				uni.showToast({
					title: '请选择手绘图',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				title: '文件上传中',
				mask: true
			});
			let scene_file = await this.$upload(tjkcxxRuleForm.scene_file);
			let report_file = await this.$upload(tjkcxxRuleForm.report_file);
			let video_file = await this.$upload(tjkcxxRuleForm.video_file);
			let sketch_file = await this.$upload(tjkcxxRuleForm.sketch_file);
			uni.hideLoading();
			data.scene_file = scene_file.arrResult;
			data.report_file = report_file.arrResult;
			data.video_file = video_file.arrResult;
			data.sketch_file = sketch_file.arrResult;
			this.submitSurveyInformation(data);
		},
		// 提交勘场报告
		submitSurveyInformation(data) {
			uni.showLoading({
				title: '信息提交中',
				mask: true
			});
			this.$Apipc.submitSurveyInformation(data).then((res) => {
				console.log(res, '提交勘场报告');
				uni.hideLoading();
				if (res.code == 200) {
					setTimeout(() => {
						var currentPages = getCurrentPages()[getCurrentPages().length - 2]
						if (currentPages) {
							uni.navigateBack({
								delta: 1
							});
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}, 500);
				}
				uni.showToast({
					title: res.code == 200 ? '提交成功' : res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
